package com.bookstore.controller;

import com.alibaba.fastjson.JSON;
import com.bookstore.entity.Category;
import com.bookstore.entity.Book;
import com.bookstore.entity.Comments;
import com.bookstore.entity.User;
import com.bookstore.service.BookService;
import com.bookstore.service.CommentService;
import com.bookstore.utils.Json;
import com.bookstore.utils.States;
import com.bookstore.utils.StringUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class CommentControl {

    @Autowired
    CommentService service;
    @Autowired
    private BookService bookService;

    /**
     * 修改是否显示数据
     * @param show
     * @param commentId
     * @return
     */
    public String updateShow(@RequestParam("show")String show,@RequestParam("commentId")String commentId){
        if (!StringUtils.isEmpty(show) & !StringUtils.isEmpty(commentId)){
            int update = service.updateShow(Integer.valueOf(show),commentId);
            if (update>0){
                return JSON.toJSONString(States.REQUEST_DEL_SUCCESS);
            }else{
                return JSON.toJSONString(States.REQUEST_DEL_FILED);
            }
        }else{
            return JSON.toJSONString(States.REQUEST_ERROR_CONDITION01);
        }
    }

    /**
     * h获取图书的评论
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/comment/get_comments_by_page", method = RequestMethod.GET,produces="text/html; charset=UTF-8")
    public String getCommentsByPage(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize,@RequestParam("bookId") String bookid) {
        if (StringUtils.isEmpty(bookid))
            return "参数bookId不能为空" ;
        PageInfo<Comments> pageInfo = service.getAllCommentsByBookId(pageNum,pageSize,bookid);
        return JSON.toJSONString(pageInfo);
    }

    /**
     * 用户添加评论
     * @param score
     * @param content
     * @param bookId
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/user/add_comment",method = RequestMethod.POST,produces="text/html; charset=UTF-8")
    @Transactional
    public String addComment(@RequestParam("score") short score, @RequestParam("content") String content,
                             @RequestParam("bookId") String bookId, HttpServletRequest request){
        User user = (User) request.getSession().getAttribute(LoginAndRegisterController.USER);
        if (user == null)
            return  JSON.toJSONString( "你还没有登录，无法进行评价");
        if (StringUtils.isEmpty(bookId))
            return  JSON.toJSONString( "评论图书的id不能为空");
        Comments comments = new Comments();
        comments.setBookId(bookId);
        comments.setDateTime(new Date());
        comments.setScore((double) score);
        comments.setUserId(user.getuId());
        comments.setInclude(content);
        bookService.countScore(bookId,score);
        if (service.addComment(comments))
            return  JSON.toJSONString( "评论提交成功！感谢你的对这本书的评价。");
        else
            return  JSON.toJSONString( "评论提交失败");
    }

    @ResponseBody
    @RequestMapping(value = "/get_comments_by_page",method = RequestMethod.GET)
    public Json getCommentsByPage(@RequestParam("pageNum")String pageNum,@RequestParam("pageSize")String pageSize){
        Json json = new Json();
        if (!StringUtils.isEmpty(pageNum) && !StringUtils.isEmpty(pageSize)) {
            if (StringUtil.isInteger(pageNum) && StringUtil.isInteger(pageSize)) {
                PageInfo<Comments> bookPageInfo = service.getCommentsByPage(Integer.valueOf(pageNum), Integer.valueOf(pageSize));
                bookPageInfo.setPrePage(Integer.parseInt(pageNum));
                bookPageInfo.setPageSize(Integer.parseInt(pageSize));
                if (Integer.parseInt(pageNum) == 1) {
                    bookPageInfo.setIsFirstPage(true);
                } else {
                    bookPageInfo.setIsFirstPage(false);
                }
                json.setObj(bookPageInfo);
                json.setMsg("数据获取成功");
                json.setSuccess(true);
                return json;
            } else {
                json.setMsg(States.REQUEST_ERROR_CONDITION02.getMsg());
                json.setSuccess(false);
                return json;
            }
        } else {
            json.setMsg(States.REQUEST_ERROR_CONDITION01.getMsg());
            json.setSuccess(false);
            return json;
        }
    }

    @ResponseBody
    @RequestMapping(value = "/del_comment",method = RequestMethod.POST)
    public Json delComments(@RequestParam("commentId")String commentId){
        Json json = new Json();
        if (!StringUtils.isEmpty(commentId)){
            boolean result = service.delComments(commentId);
            json.setSuccess(true);
            json.setMsg("删除数据成功");
            json.setObj(result);
            return json;
        }else{
            json.setMsg("删除数据失败");
            json.setSuccess(false);
            return json;
        }
    }
}
